module Ch03Test(tests) where

-- See also PlaneTest.hs.

import Test.HUnit
import Ch03

safeSecondTests func =
    TestList["empty" ~: Nothing @=? func [],
             "one" ~: Nothing @=? func [1],
             "two" ~: Just 2 @=? func [1,2],
             "three" ~: Just 2 @=? func [1,2,3]]

lengthTests func =
    TestList["empty" ~: 0 @=? func [],
             "one" ~: 1 @=? func [1],
             "many" ~: 5 @=? func [1,3,5,7,9]]

meanTests =
    TestList["one" ~: 5.0 @=? mean [5],
             "two" ~: 3.0 @=? mean [1, 5],
             "fractional" ~: 2.5 @=? mean [1,4],
             "many" ~: 6.0 @=? mean [1, 3, 7, 13]]

palindromeTests =
    TestList["empty" ~: ([] :: [Int]) @=? palindrome [],
             "one" ~: [1, 1] @=? palindrome [1],
             "many" ~: [1,2,3,3,2,1] @=? palindrome [1,2,3]]

isPalindromeTests =
    TestList["empty" ~: True @=? isPalindrome ([] :: [Int]),
             "one" ~: True @=? isPalindrome [1],
             "two different" ~: False @=? isPalindrome[1, 2],
             "two equal" ~: True @=? isPalindrome [2, 2],
             "odd equal" ~: True @=? isPalindrome [1, 5, 3, 5, 1],
             "odd different" ~: False @=? isPalindrome [1, 5, 3, 2, 1],
             "even equal" ~: True @=? isPalindrome [1, 2, 6, 6, 2, 1],
             "even different" ~: False @=? isPalindrome [1, 2, 6, 7, 2, 1]]

sortByLengthTests =
    TestList["one element" ~: [[1, 2]] @=? sortByLength [[1, 2]],
             "three in order" ~: [[], [1, 2], [1, 2, 3]] @=?
                              sortByLength [[], [1, 2], [1, 2, 3]],
             "three reversed" ~: [[], [1, 2], [1, 2, 3]] @=?
                              sortByLength [[1, 2, 3], [1, 2], []],
             "three mixed" ~: [[], [1, 2], [1, 2, 3]] @=?
                              sortByLength [[1, 2], [1, 2, 3], []]]

intersperseTests =
    TestList["empty" ~: "" @=? intersperse ',' [],
             "one element" ~: "foo" @=? intersperse ',' ["foo"],
             "two elements" ~: "foo,bar" @=? intersperse ',' ["foo", "bar"],
             "many elements" ~: "foo,bar,baz,quux" @=?
                             intersperse ',' ["foo", "bar", "baz", "quux"]]

heightTests =
    TestList["empty" ~: 0 @=? height Empty,
             "one" ~: 1 @=? height (Node "x" Empty Empty),
             "two/one" ~: 2 @=? height (Node "x" (Node "y" Empty Empty)
                                             Empty),
             "one/two" ~: 2 @=? height (Node "x" Empty
                                                 (Node "y" Empty Empty)),
             "two/two" ~: 2 @=? height (Node "x" (Node "y" Empty Empty)
                                                 (Node "z" Empty Empty)),
             "large" ~: 4 @=? height (Node "x"
                                      (Node "y" Empty
                                       (Node "z" Empty Empty))
                                      (Node "w"
                                       (Node "v" Empty
                                        (Node "u" Empty Empty))
                                       Empty))]
                                                  

tests = TestList[safeSecondTests safeSecond, safeSecondTests tidySecond,
                 lengthTests length, lengthTests myLength, meanTests,
                 palindromeTests, isPalindromeTests, sortByLengthTests,
                 intersperseTests, heightTests]
